﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RoadTeam.FineFile.Model;

namespace RoadTeam.FineFile
{
    public class ReadWrite
    {
        public static void WriteFile(string ConStr)
        {
            FileStream aFile;
            if (!File.Exists(Application.StartupPath + @"\PathInfo.txt"))
                aFile = new FileStream(@"PathInfo.txt", FileMode.Create);
            else
                aFile = new FileStream(@"PathInfo.txt", FileMode.Open);

            StreamReader sr = new StreamReader(aFile);

            List<String> LiPathInfo = new List<string>();
            while (!sr.EndOfStream)
                LiPathInfo.Add(sr.ReadLine());

            aFile.Dispose();

            List<String> liStr = LiPathInfo.FindAll(th => th.Split('|')[0] == ConStr.Split('|')[0]);

            if (liStr.Count != 0)
                LiPathInfo.RemoveAll(th => liStr.Contains(th));
            LiPathInfo.Add(ConStr);

            FileStream aWriteFile = new FileStream(@"PathInfo.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(aWriteFile);

            foreach (String item in LiPathInfo)
                sw.WriteLine(item);

            sw.Close();
        }

        public static Dictionary<String, String> ReadFile()
        {
            try
            {

                if (!File.Exists(@"PathInfo.txt"))
                {
                    File.Open(@"PathInfo.txt", FileMode.OpenOrCreate);
                }
                FileStream aFile = new FileStream(@"PathInfo.txt", FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(aFile);

                Dictionary<String, String> dict = new Dictionary<string, string>();
                String TempStr;
                while (!sr.EndOfStream)
                {
                    TempStr = sr.ReadLine();
                    dict.Add(TempStr.Split('|')[0], TempStr.Split('|')[1]);
                }

                sr.Close();
                return dict;
            }
            catch (IOException ex)
            {
                throw new Exception("读取路径失败,原因是：" + ex.Message);
            }
        }
        public static List<String> GetPathList(String Path)
        {
            StreamReader reader = new StreamReader(Path);
            List<string> li = new List<string>();
            while (!reader.EndOfStream)
            {
                li.Add(reader.ReadLine().Replace('/', '\\'));
            }
            return li;
        }

        //创建并且输出对应的更新内容以及路径
        public void PrintUpdateSummary(String TxtFilePath, List<UpdateInfo> li_UpdateInfo)
        {
            String TxtFilePaths = TxtFilePath.Substring(0, TxtFilePath.LastIndexOf("\\"));
            if (!Directory.Exists(TxtFilePaths))
                Directory.CreateDirectory(TxtFilePaths);



            FileStream aFile = new FileStream(TxtFilePath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(aFile);

            foreach (UpdateInfo item in li_UpdateInfo)
            {
                sw.WriteLine("[" + item.UpdateContent + "][" + item.UpdatePerson + "][" + item.UpdateDate + "]");
                foreach (PathInfo pi in item.Path)
                {
                    sw.WriteLine("　　" + pi.UrlPath);
                }
            }
            sw.Dispose();
        }



        public void PrintSysErrorInfo(String TxtFilePath, StringBuilder Sb)
        {
            String TxtFilePaths = TxtFilePath.Substring(0, TxtFilePath.LastIndexOf("\\"));
            if (!Directory.Exists(TxtFilePaths))
                Directory.CreateDirectory(TxtFilePaths);

            FileStream aFile = new FileStream(TxtFilePath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(aFile);


            sw.WriteLine(Sb.ToString());

            sw.Dispose();

        }





    }
}
